<?php
/**
 * 
 * User/IndexAction.class.php (设计师模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class Zforum_themeAction extends BaseAction
{

	function _initialize()
    {
		parent::_initialize();
        $_GET =get_safe_replace($_GET);


        $this->dao = M('User');
        $this->assign('bcid',0);
        $user = $this->dao->find($this->_userid);
        $this->user=$user;
        $this->assign('vo',$user);
        $this->assign('catid',7);
        $this->assign ('title','论坛_餐饮空间');
		unset($_POST['status']);
		unset($_POST['groupid']);
		unset($_POST['amount']);
		unset($_POST['point']);


    }

    public function index()
    {
        $this->assign ('seo_title','论坛首页_餐饮空间');



        $this->display();

    }

    public function index_p()
    {
        $this->assign ('seo_title','论坛_餐饮空间');



        $this->display();

    }


    public function forumlist($catid='',$module='')
    {

        $url_s='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

        $this->assign('url_s',$url_s);

        $this->Urlrule =F('Urlrule');
        if(empty($catid)) $catid =  intval($_REQUEST['id']);

        $plateid =  intval($_REQUEST['plateid']);
        $p= max(intval($_REQUEST[C('VAR_PAGE')]),1);
        if($catid){
            $cat = $this->categorys[$catid];

            $bcid = explode(",",$cat['arrparentid']);
            $jgbcid = $bcid;
            $bcid = $bcid[1];
            if($bcid == '') $bcid=intval($catid);
            if(empty($module))$module=$cat['module'];
            $this->assign('module_name',$module);
            unset($cat['id']);
            $this->assign($cat);
            $cat['id']=$catid;
            $this->assign('jgbcid',$jgbcid);
            $this->assign('catid',$catid);
            $this->assign('bcid',$bcid);
        }
        if($cat['readgroup'] && $this->_groupid!=1 && !in_array($this->_groupid,explode(',',$cat['readgroup']))){$this->assign('jumpUrl',URL('User-Login/index'));$this->error (L('NO_READ'));}
        $fields = F($this->mod[$module].'_Field');
        foreach($fields as $key=>$r){
            $fields[$key]['setup'] =string2array($fields[$key]['setup']);
        }
        $this->assign ( 'fields', $fields);


        $seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
        $this->assign ('seo_title',$seo_title);
        $this->assign ('seo_keywords',$cat['keywords']);
        $this->assign ('seo_description',$cat['description']);


        if($catid){
            $seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
            $this->assign ('seo_title',$seo_title);
            $this->assign ('seo_keywords',$cat['keywords']);
            $this->assign ('seo_description',$cat['description']);


            $where = "plateid=$plateid and top=0 ";
            if($_POST['keyword']){
                $where .= ' AND title like "%' .$_POST['keyword'] . '%" ';
                $this->assign('keyword',$_POST['keyword']);

            }

            if($_GET['order']=='essence'){
                $where .= "and essence=1";

            }
            //板块
            $plate=M('Zforum_plate')->where('id='.$plateid)->find();
            $this->assign ('plate',$plate);

            //每日统计
            $date=date('Y-m-d');
            $num=M('Zforum_record')->where("date='".$date."' and plateid=".$plateid)->find();
            $this->assign ('num',$num);
            //print_r($num);exit;
                $this->dao= M($module);
                $count = $this->dao->where($where)->count();
                //echo $this->dao->getLastSql();exit;

                if($count){
                    //import ( "@.ORG.Page" );
                    $listRows =  10;
                    $of=$listRows*($p-1);
                   // $page = new Page ( $count, $listRows );
                    //$pages = $page->forumshow();
                    $field = '*';
                    $order='updatetime desc ,createtime desc,id desc';
                    if($_GET['order']){
                        $order=$_GET['order'].' desc';
                    }
                  //  echo $order;exit;
                    $list = $this->dao->field($field)->where($where)->order($order)->limit($of , $listRows)->select();
                    //echo $this->dao->getLastSql();exit;

                    //$this->assign('pages',$pages);
                    $this->assign('list',$list);
                }

            }
        $_SESSION['rhemeurl']=$_SERVER['REQUEST_URI'];


        // echo  $_SESSION['rhemeurl'];exit;

        if($p>1){
            foreach($list as &$v){
              $v['date']=date("m月d日 H:i:s",$v['createtime']);
                $v['update']='';
               if($v['updatetime']) {
                   $v['update'] = date("m月d日 H:i:s", $v['updatetime']);
               }
            }
            $url_s=unsetParam('p', $url_s);
            $n=$p+1;
            $url=$url_s.'&p='.$n;
            $data = array('list' => $list,'plate'=>$plate ,'p' => $url);
            // print_r($data);exit;
            echo json_encode($data );//转换格式
        }else{
            $this->display();
        }
    }



    public function add()
    {
           if(!$this->_userid){

			$this->assign('jumpUrl',U('User/Login/index'));
			$this->error(L('nologin'));
			exit;
		}
        if(empty($catid)) $catid =  intval($_REQUEST['id']);
        $plateid =  intval($_REQUEST['plateid']);
        $plate=M('Zforum_plate')->where('id='.$plateid)->find();
        $this->assign ('plate',$plate);
        $this->assign('catid',$catid);

        $this->display();
    }

    public function insert()
    {
        if(!$this->_userid){

            $this->assign('jumpUrl',U('User/Login/index'));
            $this->error(L('nologin'));
            exit;
        }

        $db=M('Zforum_theme');
        $l=mb_strlen($_POST['title']);
        if ($l>90){
            $this->error(L('标题长度超过限制'));
            exit;
        }

        $content=trim (str_replace('&nbsp;','',$_POST['title']));
        if (empty($content)){
            $this->error(L('请您输入标题'));
            exit;
        }
        foreach ($_POST['pics'] AS $k => $v) {
            $img_url=tran($v);
            // echo $img_url;exit;
            $_POST['content'].='<img src="'.$img_url.'"/>' ;
        }

        $db->create($_POST);
        $db->createtime = time();
        $db->lang = LANG_ID;
        $db->userid=$this->_userid;
        $db->username=$this->user['username'];
        $db->status=1;
        $result	=$db->add();
        $plateid=$_REQUEST['plateid'];

       //添加回帖一楼
        $data_reply['content']=$_POST['content'];
        $data_reply['theme_id']=$result;
        $data_reply['createtime']=time();
        $data_reply['floor']=1;
        $data_reply['userid']=$this->_userid;
        $data_reply['username']=$this->user['username'];
        $reply=M("Zforum_reply")->add($data_reply);

        M('Zforum_plate')->where("id=".$plateid)->setInc('num'); //添加次数
        M('User')->where("id=".$this->_userid)->setInc('theme_num'); //添加次数

        //添加用户动态
        $userdate['title']='发表了【帖子】'.$_POST['title'];
        $userdate['url']='index.php?g=Home&m=Zforum_reply&a=index&id='.$result.'&plateid='.$plateid;
        $userdate['userid']=$this->_userid;
        $userdate['conid']=$result;
		$userdate['module']='Zforum_theme';
        $userdate['createtime']=time();
        $userdate['lang']='1';
        $userdate['status']='1';
        $ud=M("Dynamic")->add($userdate);

        $date=date('Y-m-d');
        $data_r['date']=$date;
        $data_r['plateid']=$plateid;
        $rr=M('Zforum_record')->where("date='".$date."' and plateid=".$plateid)->select();
       // echo M('Zforum_record')->getLastSql();exit;
        if($rr) {
            M('Zforum_record')->where("date='".$date."' and plateid=".$plateid)->setInc('num');
        }else{
            M('Zforum_record')->add( $data_r);
        }
        if($result){

            $this->success(L('发帖成功'),$_SESSION['rhemeurl']);
        }

    }





}

